package com.ruoyi.system.controller;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.domain.Rural;
import com.ruoyi.system.service.IRuralService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 乡镇成交数据Controller
 *
 * @author ruoyi
 * @date 2022-04-12
 */
@RestController
@RequestMapping("/system/rural")
public class RuralController extends BaseController {
    @Autowired
    private IRuralService ruralService;

    /**
     * 查询乡镇成交数据列表
     */
    @GetMapping("/list")
    public TableDataInfo list(Rural rural) {
        startPage();
        List<Rural> list = ruralService.selectRuralList(rural);
        return getDataTable(list);
    }

    /**
     * 获取乡镇成交数据详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Integer id) {
        return AjaxResult.success(ruralService.selectRuralById(id));
    }

    /**
     * 新增乡镇成交数据
     */
    @PostMapping
    public AjaxResult add(@RequestBody Rural rural) {
        Rural res = new Rural();
        res.setName(rural.getName());
        List<Rural> list = ruralService.selectRuralList(res);
        Assert.isTrue(list.size()==0,"已有该乡镇数据");
        rural.setCreateUser(SecurityUtils.getUserId().intValue());
        return toAjax(ruralService.insertRural(rural));
    }

    /**
     * 修改乡镇成交数据
     */
    @PutMapping
    public AjaxResult edit(@RequestBody Rural rural) {
        rural.setUpdateUser(SecurityUtils.getUserId().intValue());
        return toAjax(ruralService.updateRural(rural));
    }

    /**
     * 删除乡镇成交数据
     */
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Integer[] ids) {
        return toAjax(ruralService.deleteRuralByIds(ids));
    }
}
